package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.annotation.Transient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/role")
@CrossOrigin
public class RoleController {

    @Reference
    private RoleService roleService;



  /*  *//**
     * 保存角色信息
     *

     * @return
     *//*
    @PostMapping()
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    *//**
     * 分页查询角色信息
     *
     * @param queryPageBean
     * @return
     *//*
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询用户信息
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    *//**
     * 根据id查询角色详情
     *
     * @param id
     * @return
     *//*
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    *//**
     * 查询角色下的菜单ID集合
     *
     * @param roleId
     * @return
     *//*
    @GetMapping(path = "/{id}/menu")
    public Result selectedMenusId(@PathVariable("id") Integer roleId) {
        try {
            Integer[] menuIds = roleService.selectedMenusId(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    *//**
     * 查询角色下的权限ID集合
     *
     * @param roleId
     * @return
     *//*
    @GetMapping(path = "/{id}/permission")
    public Result selectedtPermissionsId(@PathVariable("id") Integer roleId) {
        try {
            Integer[] permissionIds = roleService.selectedPermissionsId(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    *//**
     * 更新角色信息
     *
     * @param role

     * @return
     *//*
    @PutMapping(path = "/update")
    public Result update(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.update(role, permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    *//**
     * 删除角色信息
     *
     * @param id
     * @return
     *//*
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    *//**
     * 查询所有角色信息
     *
     * @return
     *//*
    @GetMapping(path = "/all")
    public Result findAll() {
        try {
            List<Role> all = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }*/






    //查询所有角色信息
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            //1.调用service查询，返回PageResult
            pageResult = roleService.findPage(queryPageBean);
            //2.封装返回值Result
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //添加
    @PostMapping
    public Result insert(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role) {
//        try {
            roleService.add(permissionIds, menuIds, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
//        } catch (Exception e) {
//            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
//        }
  }

    //删除角色
    @DeleteMapping("/{id}")
    @Transient
    public Result deleteById(@PathVariable Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //根据id查询角色基本信息
    @GetMapping("/{id}")
    public Result findByRoleId(@PathVariable Integer id) {
        Role role = roleService.findByRoleId(id);
        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
    }

    //根据角色id查询关联的菜单id
    @GetMapping("/{id}/menu")
    public Result findMenuIdsByRoleId(@PathVariable Integer id) {
        Integer[] menuIds = roleService.findMenuIdsByRoleId(id);
        return new Result(true, menuIds);
    }

    //根据角色id查询关联的菜单id
    @GetMapping("/{id}/permission")
    public Result findPermissionIdsByRoleId(@PathVariable Integer id) {
        Integer[] permissionIds = roleService.findPermissionIdsByRoleId(id);
        return new Result(true, permissionIds);
    }

    //编辑角色信息
    @PutMapping
    @Transient
    public Result update(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role) {
//        try {
            System.out.println(permissionIds.length);
          roleService.update(permissionIds, menuIds, role);
          return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
//        } catch (Exception e) {
//            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
//        }
    }

    @GetMapping("/all")
    public Result findPage() {
        try {
            List<Role> roles= roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


}
